package com.example.shortmessage.presenter;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;

import com.example.shortmessage.contract.AccountContract;
import com.example.shortmessage.data.constant.AccountConst;
import com.example.shortmessage.data.entity.Account;
import com.example.shortmessage.data.entity.User;
import com.example.shortmessage.model.AccountModel;
import com.example.shortmessage.ui.activity.AccountActivity;
import com.example.shortmessage.utils.ActivityCollectorUtils;
import com.example.shortmessage.utils.MD5Utils;

public class AccountPresenter extends AccountContract.Presenter<AccountContract.View> {

    private Account account;
    private final AccountContract.Model accountModel;
    private User userCache = new User();

    public  AccountPresenter(@NonNull Account account) {
        accountModel = new AccountModel();
        this.account = account;
        if(account.getUser()!=null) userCache = account.getUser();
    }

    @Override
    public void release() {

    }

    @Override
    public String getTitle() {
        String title = "";
        if(account.isRegister()){
            title = AccountConst.TITLE_RE;
        }else {
            title = AccountConst.TITLE_PASSWORD;
        }
        return title;
    }

    @Override
    public String getHint() {
        if(checkViewRefIsNull()){
            return "";
        }
        String hint = "";
        User user = account.getUser();
        if(account.isRegister()){
            if(user == null || user.getUser() == null){
                hint = AccountConst.HINT_RE_USER;
            }else if(account.getPassWord() == null){
                hint = AccountConst.HINT_RE_PASSWORD;
            }else if(user.getPassword() == null){
                hint = AccountConst.HINT_RE_PASSWORD_ClONE;
            }else {
                mViewRef.get().onError(new RuntimeException("缓存账户数据异常"));
            }
        }else {
            if(user == null || user.getUser() == null){
                hint = AccountConst.HINT_PASSWORD_USER;
            }else if(account.getPassWord() == null){
                hint = AccountConst.HINT_PASSWORD_PASSWORD;
            }else if(user.getPassword() == null){
                hint = AccountConst.HINT_PASSWORD_PASSWORD_ClONE;
            }else {
                mViewRef.get().onError(new RuntimeException("缓存账户数据异常"));
            }
        }
        return hint;
    }

    @Override
    public String getButton() {
        String button = "";
        User user = account.getUser();
        if(user == null || account.getPassWord() == null){
            button = AccountConst.NEXT;
        }else {
            button = AccountConst.DONE;
        }
        return button;
    }

    @Override
    public String submit(String text, Activity activity) {
        if(checkViewRefIsNull()){
            return null;
        }

//        Account account = this.account;
//        User userCache = this.userCache;
        Account account = new Account();
        User userCache = new User();
        account.setPassWord(this.account.getPassWord());
        account.setRegister(this.account.isRegister());
        account.setUser(this.account.getUser());
        userCache.setUser(this.userCache.getUser());
        userCache.setPassword(this.userCache.getPassword());
        userCache.setId(this.userCache.getId());



        mViewRef.get().showLoading();
        User user = accountModel.query(text);
        mViewRef.get().hideLoading();
        if(account.getUser()!=null){
            if(account.getPassWord()==null){
                account.setPassWord(text);
            }else {
                if(!account.getPassWord().equals(text)) return AccountConst.FALSE_1;
                userCache.setPassword(MD5Utils.md5(text));
            }
        }else {
            if(account.isRegister()){
                if(user!=null) return AccountConst.FALSE_2;
                userCache.setUser(text);
            }else {
                if(user==null) return AccountConst.FALSE_0;
                userCache.setId(user.getId());
                userCache.setUser(user.getUser());
            }
        }
        account.setUser(userCache);
        Intent intent = new Intent(activity, AccountActivity.class);
        intent.putExtra(AccountConst.SIGN,account);
        ActivityCollectorUtils.addActivity(activity);
        if(userCache.getPassword()==null){
            activity.startActivity(intent);
        }else {
            if(account.isRegister())
                userCache.save();
            else
                userCache.update(userCache.getId());

            ActivityCollectorUtils.finishAll();
        }
        return null;
    }

}
